const http = require('../../utils/http.js')
const app = getApp()

Page({

    data: {
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        pageNum: 1,
        pageSize: 15,
        hasNextPage: false,
        itemList: []

    },

    onLoad: function () {
        let merchantCode = wx.getStorageSync('merchantCode')
        if (!merchantCode) {
            wx.redirectTo({
                url: "/pages/login/index"
            })
        }
        this.selectCouponOrder();
    },
    loginOut() {
        wx.clearStorageSync("merchantCode");
        wx.redirectTo({
            url: "/pages/login/index"
        })
    },

    scan: function (e) {
        const that = this
        wx.scanCode({
            success(res) {
                console.log(res.result)
                http.get("/api/consumer/useMerCoupon", {couponCode: res.result}).then(res=> {
                    wx.showModal({
                      title: '提示',
                      content: res.message,
                      showCancel: false
                    })
                    that.selectCouponOrder();
                })
            }
        })
    },

    selectCouponOrder() {
        const that = this
        wx.showLoading({
            title: '加载中。。。',
        })
        http.get("/api/consumer/selectCouponOrder", {pageNum: 1, pageSize: this.data.pageSize}).then(res=> {
            let listTemp = that.data.itemList;
            wx.hideLoading()
            if (res.code == 200) {
                this.setData({
                    pageNum: res.result.pageNum + 1,
                    itemList: res.result.list,
                    hasNextPage: res.result.hasNextPage,
                })
            }
        })
    },

    /**
     * 下拉刷新
     */
    onPullDownRefresh: function () {
        this.selectCouponOrder();
    },

    /**
     * 上拉分页
     */
    onReachBottom: function () {
        console.log(this.data.hasNextPage)
        const that = this
        wx.showLoading({
            title: '加载中。。。',
        })
        if (this.data.hasNextPage) {
            http.get("/api/consumer/selectCouponOrder", {pageNum: this.data.pageNum, pageSize: this.data.pageSize}).then(res=> {
                let listTemp = that.data.itemList;
                wx.hideLoading()
                if (res.code == 200) {
                    this.setData({
                        pageNum: res.result.pageNum + 1,
                        hasNextPage: res.result.hasNextPage,
                        itemList: listTemp.concat(res.result.list)
                    })
                }
            })
        } else {
            wx.showToast({
                title: '没有更多数据',
                icon: "none"
            })
        }

    },

})
